package edu.friday.repository;

import edu.friday.model.SysMenuEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SysMenuRepository extends JpaRepository<SysMenuEntity, Long> {

    @Query(value = "select m.menu_id from sys_menu m" +
            " left join sys_role_menu rm on rm.menu_id = m.menu_id" +
            " left join sys_role r on r.role_id = rm.role_id" +
            " where r.role_id = :roleId ", nativeQuery = true)
    List<Long> selectMenuIdsByRoleId(Long roleId);

    @Query(value = "select distinct m.perms from sys_menu m" +
            " left join sys_role_menu rm on m.menu_id = rm.menu_id" +
            " left join sys_user_role ur on rm.role_id = ur.role_id" +
            " where ur.user_id = :userId", nativeQuery = true)
    List<String> selectMenuPermsByUserId(@Param("userId") Long userId);

}
